package com.pactera.madp.cp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.dto.marketing.MarketingRecUpdateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingRecUpdateRankingDTO;
import com.pactera.madp.cp.api.entity.marketing.*;
import com.pactera.madp.cp.api.vo.marketing.MarketingRecVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.marketing.MarketingRecMapper;
import com.pactera.madp.cp.service.*;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: yuShen.qi
 * @Date: 2021/5/25 16:23
 * @Description:
 */
@Service
@Data
public class MarketingRecServiceImpl extends ServiceImpl<MarketingRecMapper,MarketingRecEntity> implements MarketingRecService {

    private final MarketingRecMapper marketingRecMapper;
    private final MarketingArticleService marketingArticleService;
    private final MarketingLinkService marketingLinkService;
    private final MarketingPdfService marketingPdfService;
    private final MarketingPptService marketingPptService;
    private final MarketingWordService marketingWordService;
    private final MarketingExcelService marketingExcelService;
    private final MarketingPosterService marketingPosterService;

    @Override
    public IPage allList(String title,Page page) {
        Integer corpId = CorpContextHolder.getCorpId();
        return marketingRecMapper.allList(page,title,corpId);
    }


    @Override
    public boolean changeList(String title, Integer type,Integer corpId,Integer classification) {
        //通过类型判断，调用对应实体类的方法并返回值，1:文章，2:链接，3:海报，4:pdf，5:ppt，6:excel，7:word。
        MarketingRecEntity one = getOne(Wrappers.<MarketingRecEntity>lambdaQuery()
                .eq(MarketingRecEntity::getCorpId, corpId)
                .orderByDesc(MarketingRecEntity::getId).last("limit 1"));
        if(null==one){
            MarketingRecEntity marketingRecEntity = new MarketingRecEntity();
            marketingRecEntity.setSort(1);
            marketingRecEntity.setCorpId(corpId);
            save(marketingRecEntity);
        }
        assert one != null;
        MarketingRecEntity two = getOne(Wrappers.<MarketingRecEntity>lambdaQuery()
                .eq(MarketingRecEntity::getCorpId, corpId)
                .orderByDesc(MarketingRecEntity::getId).last("limit 1"));
        Integer sort = two.getSort();
        MarketingRecEntity marketingRecEntity = new MarketingRecEntity();
        marketingRecEntity.setSort(sort+1);
        marketingRecEntity.setCorpId(corpId);
        save(marketingRecEntity);
        switch (type) {
            case 1:
                MarketingArticleEntity article= marketingArticleService.getOne(Wrappers.<MarketingArticleEntity>lambdaQuery()
                        .eq(MarketingArticleEntity::getTitle, title)
                        .eq(MarketingArticleEntity::getCorpId,corpId)
                        .eq(MarketingArticleEntity::getClassification,classification));
                //选中数据推荐状态改为已推荐
                MarketingArticleEntity marketingArticleEntity = new MarketingArticleEntity();
                marketingArticleEntity.setRecState(1);
                marketingArticleEntity.setSort(sort);
                marketingArticleEntity.setUpdateTime(LocalDateTime.now());
            return     marketingArticleService.update(marketingArticleEntity,Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getId,article.getId()).eq(MarketingArticleEntity::getClassification,classification));
            case 2:
                MarketingLinkEntity link = marketingLinkService.getOne(Wrappers.<MarketingLinkEntity>lambdaQuery()
                        .eq(MarketingLinkEntity::getTitle, title).eq(MarketingLinkEntity::getCorpId,corpId));
                //选中数据推荐状态改为已推荐
                MarketingLinkEntity marketingLinkEntity = new MarketingLinkEntity();
                marketingLinkEntity.setRecState(1);
                marketingLinkEntity.setSort(sort);
                marketingLinkEntity.setUpdateTime(LocalDateTime.now());
             return    marketingLinkService.update(marketingLinkEntity,Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId,link.getId()));
            case 3:
                MarketingPosterEntity poster = marketingPosterService.getOne(Wrappers.<MarketingPosterEntity>lambdaQuery()
                        .eq(MarketingPosterEntity::getTitle, title).eq(MarketingPosterEntity::getCorpId,corpId));
                //选中数据推荐状态改为已推荐
                MarketingPosterEntity marketingPosterEntity = new MarketingPosterEntity();
                marketingPosterEntity.setRecState(1);
                marketingPosterEntity.setSort(sort);
                marketingPosterEntity.setUpdateTime(LocalDateTime.now());
                return    marketingPosterService.update(marketingPosterEntity,Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId,poster.getId()));
            case 4:
                MarketingPdfEntity pdf = marketingPdfService.getOne(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getTitle, title).eq(MarketingPdfEntity::getCorpId,corpId));
                MarketingPdfEntity marketingPdfEntity = new MarketingPdfEntity();
                marketingPdfEntity.setRecState(1);
                marketingPdfEntity.setSort(sort);
                marketingPdfEntity.setUpdateTime(LocalDateTime.now());
             return    marketingPdfService.update(marketingPdfEntity,Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId,pdf.getId()));

            case 5:
                MarketingPptEntity ppt = marketingPptService.getOne(Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getTitle, title).eq(MarketingPptEntity::getCorpId,corpId));
                MarketingPptEntity marketingPptEntity = new MarketingPptEntity();
                marketingPptEntity.setRecState(1);
                marketingPptEntity.setSort(sort);
                marketingPptEntity.setUpdateTime(LocalDateTime.now());
             return    marketingPptService.update(marketingPptEntity,Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getId,ppt.getId()));
            case 6:
                MarketingExcelEntity excelEntity = marketingExcelService.getOne(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getTitle, title).eq(MarketingExcelEntity::getCorpId,corpId));
                MarketingExcelEntity marketingExcelEntity = new MarketingExcelEntity();
                marketingExcelEntity.setRecState(1);
                marketingExcelEntity.setSort(sort);
                marketingExcelEntity.setUpdateTime(LocalDateTime.now());
              return   marketingExcelService.update(marketingExcelEntity,Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId,excelEntity.getId()));
            case 7:
                MarketingWordEntity word = marketingWordService.getOne(Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getTitle, title).eq(MarketingWordEntity::getCorpId,corpId));
                MarketingWordEntity marketingWordEntity = new MarketingWordEntity();
                marketingWordEntity.setRecState(1);
                marketingWordEntity.setSort(sort);
                marketingWordEntity.setUpdateTime(LocalDateTime.now());
             return    marketingWordService.update(marketingWordEntity,Wrappers.<MarketingWordEntity>lambdaQuery()
                     .eq(MarketingWordEntity::getId,word.getId()));
            default:
                return true;
        }


    }

    @Override
    public IPage listByType(Page page,String title, Integer type) {
        //通过类型判断，调用对应实体类的方法并返回值，1:文章，2:链接，3:海报，4:pdf，5:ppt，6:excel，7:word。
        Integer corpId = CorpContextHolder.getCorpId();
        switch (type) {
            case 1:
                QueryWrapper<MarketingArticleEntity> wrapper1 = new QueryWrapper<>();
                wrapper1.select("id","title","type","ar_attribute","classification","update_time")
                        .like("title",title)
                        .eq("corp_id",corpId)
                        .eq("rec_state",2)
                        .eq("marketing_state",2)
                        .orderByDesc("update_time");
                return marketingArticleService.page(page,wrapper1);
            case 2:
                QueryWrapper<MarketingLinkEntity> wrapper2 = new QueryWrapper<>();
                wrapper2.select("id","title","type","update_time")
                        .like("title",title).eq("corp_id",corpId)
                        .eq("rec_state",2)
                        .eq("marketing_state",2)
                        .orderByDesc("update_time");
                return marketingLinkService.page(page,wrapper2);
            case 3:
                QueryWrapper<MarketingPosterEntity> wrapper3 = new QueryWrapper<>();
                wrapper3.select("id","title","type","update_time")
                        .like("title",title).eq("corp_id",corpId)
                        .eq("rec_state",2)
                        .eq("marketing_state",2)
                        .orderByDesc("update_time");
                return marketingPosterService.page(page,wrapper3);
            case 4:
                QueryWrapper<MarketingPdfEntity> wrapper4 = new QueryWrapper<>();
                wrapper4.select("id","title","type","update_time")
                        .like("title",title)
                        .eq("corp_id",corpId)
                        .eq("rec_state",2)
                        .eq("marketing_state",2)
                        .orderByDesc("update_time");
                return marketingPdfService.page(page,wrapper4);
            case 5:
                QueryWrapper<MarketingPptEntity> wrapper5 = new QueryWrapper<>();
                wrapper5.select("id","title","type","update_time")
                        .like("title",title)
                        .eq("corp_id",corpId)
                        .eq("rec_state",2)
                        .eq("marketing_state",2)
                        .orderByDesc("update_time");
                return marketingPptService.page(page,wrapper5);
            case 6:
                QueryWrapper<MarketingExcelEntity> wrapper6 = new QueryWrapper<>();
                wrapper6.select("id","title","type","update_time")
                        .like("title",title)
                        .eq("corp_id",corpId)
                        .eq("rec_state",2)
                        .eq("marketing_state",2)
                        .orderByDesc("update_time");
                return marketingExcelService.page(page,wrapper6);
            case 7:
                QueryWrapper<MarketingWordEntity> wrapper7 = new QueryWrapper<>();
                wrapper7.select("id","title","type","update_time")
                        .like("title",title).eq("corp_id",corpId)
                        .eq("rec_state",2)
                        .eq("marketing_state",2)
                        .orderByDesc("update_time");
                return marketingWordService.page(page,wrapper7);
            default:
                return null;
        }
    }

    @Override
    public boolean deleteById(Integer id,Integer type,String title,Integer classification) {
        switch (type) {
            case 1:
                //选中数据推荐状态改为未推荐
                MarketingArticleEntity marketingArticleEntity = new MarketingArticleEntity();
                marketingArticleEntity.setRecState(2);
                marketingArticleEntity.setUpdateTime(LocalDateTime.now());
               return marketingArticleService.update(marketingArticleEntity,Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getTitle,title).eq(MarketingArticleEntity::getClassification,classification));
            case 2:
                //选中数据推荐状态改为未推荐
                MarketingLinkEntity marketingLinkEntity = new MarketingLinkEntity();
                marketingLinkEntity.setRecState(2);
                marketingLinkEntity.setUpdateTime(LocalDateTime.now());
               return marketingLinkService.update(marketingLinkEntity,Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getTitle,title).eq(MarketingLinkEntity::getClassification,classification));
            case 3:
                //选中数据推荐状态改为未推荐
                MarketingPosterEntity marketingPosterEntity = new MarketingPosterEntity();
                marketingPosterEntity.setRecState(2);
                marketingPosterEntity.setUpdateTime(LocalDateTime.now());
                return marketingPosterService.update(marketingPosterEntity,Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getTitle,title).eq(MarketingPosterEntity::getClassification,classification));
           case 4:
                MarketingPdfEntity marketingPdfEntity = new MarketingPdfEntity();
                marketingPdfEntity.setRecState(2);
                marketingPdfEntity.setUpdateTime(LocalDateTime.now());
                return    marketingPdfService.update(marketingPdfEntity,Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getTitle,title).eq(MarketingPdfEntity::getClassification,classification));

            case 5:
                MarketingPptEntity marketingPptEntity = new MarketingPptEntity();
                marketingPptEntity.setRecState(2);
                marketingPptEntity.setUpdateTime(LocalDateTime.now());
                return   marketingPptService.update(marketingPptEntity,Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getTitle,title).eq(MarketingPptEntity::getClassification,classification));

            case 6:
                MarketingExcelEntity marketingExcelEntity = new MarketingExcelEntity();
                marketingExcelEntity.setRecState(2);
                marketingExcelEntity.setUpdateTime(LocalDateTime.now());
                return    marketingExcelService.update(marketingExcelEntity,Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getTitle,title).eq(MarketingExcelEntity::getClassification,classification));
            case 7:
            MarketingWordEntity marketingWordEntity = new MarketingWordEntity();
            marketingWordEntity.setRecState(2);
                marketingWordEntity.setUpdateTime(LocalDateTime.now());
            return    marketingWordService.update(marketingWordEntity,Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getTitle,title).eq(MarketingWordEntity::getClassification,classification));
             default:
                return true;
        }
    }

    @Override
    public List<MarketingRecVO> allListByPage(String title) {
        Integer corpId = CorpContextHolder.getCorpId();
        return marketingRecMapper.lookListByPage(title,corpId);
    }

    @Override
    public List<MarketingRecVO> allListByPageAndType( String title, Integer type) {
        Integer corpId = CorpContextHolder.getCorpId();
        return marketingRecMapper.lookListByType(title,corpId,type);
    }

    @Override
    public List<MarketingRecVO> allListWechat(Page page,Integer type,String title) {
        Integer corpId = CorpContextHolder.getCorpId();
        return marketingRecMapper.chooseList(title, corpId,type);
    }

    @Override
    public void changeRanking(MarketingRecUpdateDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        List<MarketingRecUpdateRankingDTO> marketingRecRanking = dto.getMarketingRecRanking();
        marketingRecRanking.forEach(entity->{
            switch (entity.getType()) {
                case 1:
                    MarketingArticleEntity article= marketingArticleService.getOne(Wrappers.<MarketingArticleEntity>lambdaQuery()
                            .eq(MarketingArticleEntity::getTitle, entity.getTitle()).eq(MarketingArticleEntity::getCorpId,corpId));
                    //选中数据推荐状态改为已推荐
                    MarketingArticleEntity marketingArticleEntity = new MarketingArticleEntity();
                    marketingArticleEntity.setSort(entity.getSort());
                    marketingArticleEntity.setUpdateTime(LocalDateTime.now());
                         marketingArticleService.update(marketingArticleEntity,Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getId,article.getId()));
                    break;
                case 2:
                    MarketingLinkEntity link = marketingLinkService.getOne(Wrappers.<MarketingLinkEntity>lambdaQuery()
                            .eq(MarketingLinkEntity::getTitle, entity.getTitle()).eq(MarketingLinkEntity::getCorpId,corpId));
                    //选中数据推荐状态改为已推荐
                    MarketingLinkEntity marketingLinkEntity = new MarketingLinkEntity();
                    marketingLinkEntity.setSort(entity.getSort());
                    marketingLinkEntity.setUpdateTime(LocalDateTime.now());
                       marketingLinkService.update(marketingLinkEntity,Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId,link.getId()));
                    break;
                case 3:
                    MarketingPosterEntity poster = marketingPosterService.getOne(Wrappers.<MarketingPosterEntity>lambdaQuery()
                            .eq(MarketingPosterEntity::getTitle, entity.getTitle()).eq(MarketingPosterEntity::getCorpId,corpId));
                    //选中数据推荐状态改为已推荐
                    MarketingPosterEntity marketingPosterEntity = new MarketingPosterEntity();
                    marketingPosterEntity.setSort(entity.getSort());
                    marketingPosterEntity.setUpdateTime(LocalDateTime.now());
                    marketingPosterService.update(marketingPosterEntity,Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId,poster.getId()));
                    break;
                case 4:
                    MarketingPdfEntity pdf = marketingPdfService.getOne(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getTitle, entity.getTitle()).eq(MarketingPdfEntity::getCorpId,corpId));
                    MarketingPdfEntity marketingPdfEntity = new MarketingPdfEntity();
                    marketingPdfEntity.setSort(entity.getSort());
                    marketingPdfEntity.setUpdateTime(LocalDateTime.now());
                        marketingPdfService.update(marketingPdfEntity,Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId,pdf.getId()));
                    break;
                case 5:
                    MarketingPptEntity ppt = marketingPptService.getOne(Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getTitle, entity.getTitle()).eq(MarketingPptEntity::getCorpId,corpId));
                    MarketingPptEntity marketingPptEntity = new MarketingPptEntity();
                    marketingPptEntity.setSort(entity.getSort());
                    marketingPptEntity.setUpdateTime(LocalDateTime.now());
                        marketingPptService.update(marketingPptEntity,Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getId,ppt.getId()));
                    break;
                case 6:
                    MarketingExcelEntity excelEntity = marketingExcelService.getOne(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getTitle, entity.getTitle()).eq(MarketingExcelEntity::getCorpId,corpId));
                    MarketingExcelEntity marketingExcelEntity = new MarketingExcelEntity();
                    marketingExcelEntity.setSort(entity.getSort());
                    marketingExcelEntity.setUpdateTime(LocalDateTime.now());
                       marketingExcelService.update(marketingExcelEntity,Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId,excelEntity.getId()));
                    break;
                case 7:
                    MarketingWordEntity word = marketingWordService.getOne(Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getTitle, entity.getTitle()).eq(MarketingWordEntity::getCorpId,corpId));
                    MarketingWordEntity marketingWordEntity = new MarketingWordEntity();
                    marketingWordEntity.setSort(entity.getSort());
                    marketingWordEntity.setUpdateTime(LocalDateTime.now());
                        marketingWordService.update(marketingWordEntity,Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getId,word.getId()));
                    break;
                default:
                    break;
            }
        });

        /*for (MarketingRecUpdateRankingDTO o :marketingRecRanking) {
            MarketingRecEntity marketingRecEntity = new MarketingRecEntity();
            remove(Wrappers.<MarketingRecEntity>lambdaQuery().eq(MarketingRecEntity::getId,o.getId()));
            BeanUtil.copyProperties(o,marketingRecEntity);
            marketingRecEntity.setUpdateTime(LocalDateTime.now());
            save(marketingRecEntity);
        }*/
    }

}
